package cn.kgc.ssm.service.impl;

import cn.kgc.ssm.entity.User;
import cn.kgc.ssm.mapper.UserMapper;
import cn.kgc.ssm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    @Override
    public List<User> selectUserAll() {
        return userMapper.selectUserAll();
    }

    @Override
    public User selectUserById(Integer id) {
        return userMapper.selectUserById(id);
    }

    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    @Override
    public User loginUser(User user) {
        return userMapper.loginUser(user);
    }

    @Override
    public int addUser(User user) {
        return 0;
    }

    @Override
    public int updataUser(User user) {
        return 0;
    }

    @Override
    public int delUser(User user) {
        return 0;
    }

    @Transactional
    @Override
    public int transfer(User fromUser, User toUser, Double cash) {

        fromUser.setCash(fromUser.getCash() - cash);
        toUser.setCash(toUser.getCash() + cash);
        userMapper.updataUser(fromUser);
        int i = 1/0;
        userMapper.updataUser(toUser);


        return 0;
    }
}
